package fr.pud.client.controller.query;

// QueryFrame.getInstance().getButtonAdd().setVisible(true);
// QueryFrame.getInstance().getButtonApplyModification().setVisible(false);
// Query query=(Query)
// QueryFrame.getInstance().getQueriesInformation().getList().getSelectedValue();
// query.setQuantity(Integer.parseInt(QueryFrame.getInstance().getDataPanel().getTextQuantity().getText()));
// query.setQuantity(QueryFrame)
import java.awt.event.ActionEvent;

import javax.swing.AbstractAction;

import fr.pud.client.i18n.LanguagesConstants;
import fr.pud.client.images.ImagesDatabase;
import fr.pud.client.model.PUDStation;
import fr.pud.client.model.Queries;
import fr.pud.client.model.Query;
import fr.pud.client.model.Time;
import fr.pud.client.view.QueryFrame;

/**
 * Apr 6, 2012 - tsp-osmBis
 * @author Simon Devineau, Adrien Hierry, Leopold Houdin, Pierre Reliquet
 *         Ecole des Mines de Nantes
 *         ApplyModificationQueries.java
 */
public class ApplyModificationQueries extends AbstractAction {
    /**
     * 
     */
    private static final long serialVersionUID = 7086153494703490016L;

    public ApplyModificationQueries() {
        this.putValue(NAME, LanguagesConstants.LANGUAGE
                .getString(LanguagesConstants.APPLY_MODIFICATIONS));
        this.putValue(SHORT_DESCRIPTION, LanguagesConstants.LANGUAGE
                .getString(LanguagesConstants.APPLY_MODIFICATIONS));
        this.putValue(SMALL_ICON, ImagesDatabase.EDIT);
    }

    /*
     * (non-Javadoc)
     * @see
     * java.awt.event.ActionListener#actionPerformed(java.awt.event.ActionEvent)
     */
    @Override
    public void actionPerformed(ActionEvent arg0) {
        QueryFrame.getInstance().getButtonAdd().setVisible(true);
        QueryFrame.getInstance().getButtonApplyModification().setVisible(false);
        String textQuantity = QueryFrame.getInstance().getDataPanel()
                .getTextQuantity().getText();
        String textMaxDriveDuration = QueryFrame.getInstance().getDataPanel()
                .getTextMaxDrive().getText();
        String loadingTimePickUp = QueryFrame.getInstance().getDataPanel()
                .getTextLoadingTimePickUp().getText();
        String loadingTimeDelivery = QueryFrame.getInstance().getDataPanel()
                .getTextLoadingTimeDelivery().getText();
        String textOpeningTimePickup = QueryFrame.getInstance().getDataPanel()
                .getTextOpeningTimePickup().getText();
        String textClosingTimePickup = QueryFrame.getInstance().getDataPanel()
                .getTextClosingTimePickup().getText();
        String textOpeningTimeDelivery = QueryFrame.getInstance()
                .getDataPanel().getTextOpeningTimeDelivery().getText();
        String textClosingTimeDelivery = QueryFrame.getInstance()
                .getDataPanel().getTextClosingTimeDelivery().getText();
        if (OpenQueryFrame.checkInputUser(textQuantity, textMaxDriveDuration,
                loadingTimePickUp, loadingTimeDelivery, textOpeningTimePickup,
                textClosingTimePickup, textOpeningTimeDelivery,
                textClosingTimeDelivery)) {
            // Get the querie selected by the user
            Query q = (Query) QueryFrame.getInstance().getListQueries()
                    .getSelectedValue();
            // Modify the quantity and the max drive duration according to the
// input.
            q.setQuantity(Integer.parseInt(textQuantity));
            q.setMaxDriveDuration(Integer.parseInt(textMaxDriveDuration));
            // Modify the pickup
            PUDStation pickup = (PUDStation) QueryFrame.getInstance()
                    .getDataPanel().getPUDStationPickUp().getSelectedItem();
            pickup.setClosing(new Time(textClosingTimePickup));
            pickup.setOpening(new Time(textOpeningTimePickup));
            pickup.setLoadingTime(Integer.parseInt(loadingTimePickUp));
            // Modify the delivery
            PUDStation delivery = (PUDStation) QueryFrame.getInstance()
                    .getDataPanel().getPUDStationDelivery().getSelectedItem();
            delivery.setClosing(new Time(textClosingTimeDelivery));
            delivery.setOpening(new Time(textOpeningTimeDelivery));
            delivery.setLoadingTime(Integer.parseInt(loadingTimeDelivery));
            // Modify the delviery and pickup for the queries
            q.setDelivery(delivery);
            q.setPickup(pickup);
            Queries.getInstance().setQueries();
            // Make button visible
            QueryFrame.getInstance().getButtonAdd().setVisible(true);
            QueryFrame.getInstance().getButtonApplyModification()
                    .setVisible(false);
            System.out.println(Queries.getInstance());
        }
        else {
            QueryFrame.getInstance().getButtonAdd().setVisible(false);
            QueryFrame.getInstance().getButtonApplyModification()
                    .setVisible(true);
        }
    }
}
